package com.liuhouse.utils;

import com.liuhouse.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

//在"发送评论"功能哪里会用到
public class SecurityUtils {


    /**
     * 获取Authentication
     * 获取授权信息
     * @return
     */
    public static Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }


    /**
     * 获取用户的信息
     * @return
     */
    public static LoginUser getLoginUser(){
        return (LoginUser) getAuthentication().getPrincipal();
    }


    /**
     * 指定userid的用户就是网站的管理员
     * @return
     */
    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null && 1L == id;
    }

    /**
     * 获取用户id
     * @return
     */
    public static Long getUserId(){
        return getLoginUser().getUser().getId();
    }

}
